﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.ComponentModel;
using System.Windows.Input;
using ZFCNew.Code;
using System.Linq;

namespace ZFCNew.ViewModels
{
    public class TwitterSearchModel : INotifyPropertyChanged
    {
        private const string SearchApi = "http://search.twitter.com/search.json?q={0}&rpp={1}&page={2}";
        private const int SearchPageSize = 20;
        private readonly GenericSearch<TwitterSearchData> _twitterSearch;
        private const int PageSize = 4;
        private int _currentPage;
        private string searchString;

        public bool IsLoggedIn { get { return false; } }

        public TwitterSearchModel()
        {
            _twitterSearch = new GenericSearch<TwitterSearchData>(SearchApi);
            _twitterSearch.SearchCompleted += TwitterSearchSearchCompleted;
            searchString = "Zakumi";
            CurrentResult = new List<TwitterSearchResult>();
            GetNextResultSetCommand = new DelegateCommand(GetNextGeneralSearch, (s) => true);
            GetPreviousResultSetCommand = new DelegateCommand(GetPrevGeneralSearch, (s) => true);
            GeneralSearchCommand = new DelegateCommand(GeneralSearch, (s) => true);
            GeneralSearchResult = new TwitterSearchData
            {
                Results = new List<TwitterSearchResult>()
            };
           
        }

        void TwitterSearchSearchCompleted(object sender, SearchBaseEventArgs<TwitterSearchData> e)
        {
            Deployment.Current.Dispatcher.BeginInvoke(()=>News.Loaer.Visibility = Visibility.Collapsed);
            AdjustSearchResults(e.Results);
        }




       

        public List<TwitterSearchResult> CurrentResult { get; private set; }
        public ICommand GetNextResultSetCommand { get; private set; }
        public ICommand GetPreviousResultSetCommand { get; private set; }
        public ICommand GeneralSearchCommand { get; private set; }

        public TwitterSearchData GeneralSearchResult { get; private set; }




        private void GetNextGeneralSearch(object o)
        {
            _currentPage++;
            CurrentResult = GeneralSearchResult.Results.Skip(_currentPage * PageSize).Take(PageSize).ToList();

            if (CurrentResult.Count < PageSize)
            {
                  _twitterSearch.Search(searchString,SearchPageSize,_currentPage);
                  Deployment.Current.Dispatcher.BeginInvoke(()=>News.Loaer.Visibility = Visibility.Visible);
            }
            Deployment.Current.Dispatcher.BeginInvoke(() => NotifyPropertyChanged("CurrentResult"));
            

        }

        private void GetPrevGeneralSearch(object o)
        {
            if (_currentPage > 0)
            {
                _currentPage--;
                CurrentResult = GeneralSearchResult.Results.Skip(_currentPage * PageSize).Take(PageSize).ToList();
                Deployment.Current.Dispatcher.BeginInvoke(() => NotifyPropertyChanged("CurrentResult"));
            }
        }

        //private void TwitterHelper_SearchCompleted(object sender, TwitterSearchEventArgs e)
        //{
        //    AdjustSearchResults(e.Results);
        //}

        private void AdjustSearchResults(TwitterSearchData res)
        {
            if (GeneralSearchResult.Results.Count > 200)
            {
                GeneralSearchResult.Results.Clear();
            }
            GeneralSearchResult.Results.AddRange(res.Results);
            if (CurrentResult.Count < PageSize)
            {
                CurrentResult = GeneralSearchResult.Results.Skip(_currentPage * PageSize).Take(PageSize).ToList();
            }
            Deployment.Current.Dispatcher.BeginInvoke(() => NotifyPropertyChanged("GeneralSearchResult", "CurrentResult"));
        }

        public void GeneralSearch(object keywords)
        {
            if (keywords is string && !string.IsNullOrEmpty(keywords.ToString()))
                searchString = keywords.ToString();
            GeneralSearchResult.Results.Clear();
            CurrentResult.Clear();
            _currentPage = 0;
            Deployment.Current.Dispatcher.BeginInvoke(() => NotifyPropertyChanged("GeneralSearchResult", "CurrentResult"));
            this.searchString = searchString.ToString();
            _twitterSearch.Search(searchString, SearchPageSize, 1);
            Deployment.Current.Dispatcher.BeginInvoke(()=>News.Loaer.Visibility = Visibility.Visible);
        }

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(params String[] info)
        {
            if (PropertyChanged != null)
            {
                foreach (var s in info)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(s));
                }

            }
        }

        #endregion
    }
}